package com.nsapi.niceschoolapi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsapi.niceschoolapi.common.base.PageData;
import com.nsapi.niceschoolapi.common.config.MySysUser;
import com.nsapi.niceschoolapi.common.util.Constants;
import com.nsapi.niceschoolapi.common.util.Encodes;
import com.nsapi.niceschoolapi.common.util.ResponseEntity;
import com.nsapi.niceschoolapi.entity.*;
import com.nsapi.niceschoolapi.service.CourseService;
import com.nsapi.niceschoolapi.service.PoliticsService;
import com.nsapi.niceschoolapi.service.StudentService;
import com.nsapi.niceschoolapi.service.UserService;
import org.springframework.dao.support.DaoSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.naming.ldap.PagedResultsControl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Resource
    private PoliticsService politicsService;

    @Resource
    private StudentService studentService;

    @Resource
    private UserService userService;

    @Resource
    private CourseService courseService;
    //进入学生录入页面
    @RequestMapping(value = "/addStudentPage")
    public String addStudentPage(Model model){
        List<PoliticsTypeDB> politicsList=politicsService.politicsList();
        model.addAttribute("stupol",politicsList);
        return "view/student/addStudent";
    }

    //进行添加学生信息
    @RequestMapping(value = "/addStudent")
    @ResponseBody
    public StudentVO addStudent(StudentVO studentVO,String birthday,String tertime) throws ParseException {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        studentVO.setSbirthday(format.parse(birthday));
        int inFlag=(int)(Math.random()*1000000); //生成学号
        studentVO.setStuid(String.valueOf(inFlag));
        studentVO.setEntertime(new Date());
        //添加学生信息
        int result=studentService.addStudent(studentVO);
        //  班级人数+1
        Integer selClassinfo = studentService.selecteClassinfo(studentVO.getClassid());
        //  年级人数+1
        Integer selGrade = studentService.selecteGrade(studentVO.getGid());
        //  专业人数+1
        Integer selMajor = studentService.selecteMajor(studentVO.getMid());
        //  系部人数+1
        Integer selDepartment = studentService.selecteDepartment(studentVO.getDid());

        return studentVO;
    }
    //添加角色为学生的用户
    @RequestMapping(value ="student/user/add")
    @ResponseBody
    public Boolean userAdd(@RequestBody User user){
        user.setCreateDate(new Date()); //创建时间
        user.setCreateId(Constants.userId);
        user.setDelFlag(false);
        //添加用户信息
        userService.saveUser(user);
        //按角色名称教师信息
        Role role=politicsService.getRoleByName("学生");
        //保存用户角色关系
        UserRole userRole=new UserRole(user.getId(),role.getId());
        userService.addUserRole(userRole);
        return true;
    }
    //进入学生信息查询页面
    @RequestMapping(value = "/selectStudent")
    public String selectStudent(){
        return "view/student/selStudent";
    }

    //异步查询学生信息
    @RequestMapping(value = "/selStudent")
    @ResponseBody
    public LayuiResult<Map> selectStu( @RequestParam(value = "page",defaultValue = "1") Integer page,
                                       @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                       StudentVO studentVO){
        LayuiResult<Map> result = new LayuiResult<>();
        PageHelper.startPage(page,limit);
        List<Map> list = studentService.selStudent(studentVO);
        PageInfo pageInfo=new PageInfo(list);
        result.setData(pageInfo.getList());
        result.setCount(pageInfo.getTotal());
        return result;
    }

    //查看学生详细信息
    @RequestMapping(value = "/selectMessage")
    public String  selectMessage(Integer id,Model model){
        //查询所有政治面貌信息
        List<PoliticsTypeDB> politicsList=politicsService.politicsList();
        model.addAttribute("stupol",politicsList);
        //根据id查询学生详细信息
        StudentDB studentDB=studentService.selStudentId(id);
        //查询所有班级信息
        List<ClassinfoDB> classinfoDBS=studentService.selClass();
        model.addAttribute("cls",classinfoDBS);
        model.addAttribute("stu",studentDB);
        return "view/student/selStuExam";
    }

    //修改学生信息
    @RequestMapping(value = "/selStudentId")
    public String selStudentId(Integer id,Model model){
        //查询所有政治面貌信息
        List<PoliticsTypeDB> politicsList=politicsService.politicsList();
        model.addAttribute("stupol",politicsList);
        //根据id查询学生详细信息
        StudentDB studentDB=studentService.selStudentId(id);
        //查询所有班级信息
        List<ClassinfoDB> classinfoDBS=studentService.selClass();
        model.addAttribute("cls",classinfoDBS);
        model.addAttribute("stu",studentDB);
        return "view/student/updStudent";
    }

    //修改学生信息
    @RequestMapping(value = "/updateStudent")
    @ResponseBody
    public ResponseEntity updateStudent(StudentDB studentDB, String birthday) throws ParseException {
        ResponseEntity response=new ResponseEntity();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        studentDB.setSbirthday(format.parse(birthday));
        int reuslt=studentService.updateStudent(studentDB);
        if (reuslt>0){
            response.setAny("msg","修改成功！");
        }else{
            response.setAny("msg","修改失败！");
        }
        return response;
    }

    //进入退学页面
    @RequestMapping(value = "/dropoutPage")
    public String dropoutPage(){
        return "view/student/selDropout";
    }
    //查询退学信息
    @RequestMapping(value = "/selDropout")
    @ResponseBody
    public PageData<DropoutVO> selDropout( @RequestParam(value = "page",defaultValue = "1") Integer page,
                                           @RequestParam(value = "limit" , defaultValue = "10") Integer limit,
                                           DropoutVO dropoutVO){
        PageData<DropoutVO> pageData=new PageData<>();
        PageHelper.startPage(page,limit);
        List<DropoutVO> dropoutVOS=studentService.selDropout(dropoutVO);
        PageInfo pageInfo=new PageInfo(dropoutVOS);
        pageData.setData(pageInfo.getList());
        pageData.setCount(pageInfo.getTotal());
        return pageData;
    }

    //进入退学页面
    @RequestMapping(value = "/insertDropout")
    public String insertDropout(Model model,Integer stuid,String sname,Integer id){
        model.addAttribute("stuid" ,stuid);
        model.addAttribute("sname",sname);
        model.addAttribute("sid",id);
        return "view/student/dropout";
    }

    //进行退学操作
    @RequestMapping(value = "/delStudent")
    @ResponseBody
    public ResponseEntity delStudent(DropoutVO dropoutVO){
        ResponseEntity response=new ResponseEntity();
         dropoutVO.setLeavetime(new Date());
        int result=studentService.studentDropout(dropoutVO);
        if (result>0){
            result=studentService.addDropout(dropoutVO);
            if (result>0){
                response.setAny("msg","退学成功！");
            }
        }else{
            response.setAny("msg","退学失败！");
        }
        return response;
    }
}
